package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.authority.Party;
import com.example.mapp.IndexEchartsMapp;
import com.example.mapp.PartyMapp;
import com.example.mapp.StatisticsMapp;
import com.example.service.IndexEchartsServices;
import com.example.util.ResultUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexEchartsServicesImpl implements IndexEchartsServices {
    private final static Logger logger = (Logger) LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    IndexEchartsMapp indexEchartsMapp;

    @Autowired
    PartyMapp partyMapp;

    @Autowired
    StatisticsMapp statisticsMapp;



    @Override
    public Map<String, Object> countTypeDistribType(Map map) {
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            if (map != null && map.size() > 0) {
                int integer = (int) map.get("id");
                String id = String.valueOf(integer);

                // 通过id判断是市级单位还是局已经分局
                String limit = decideInfo(id);
                if ("0".equals(limit)) {
                    // 0市
                    list = indexEchartsMapp.countTypeDistribTypeCity(id);
                } else {
                    // 1、2 局和分局看局
                    if ("2".equals(limit)) {
                        id = partyMapp.infoById(id).getParentId();
                    }
                    list = indexEchartsMapp.countTypeDistribTypeArea(id);
                }
            }

            Long totalNum = 0l;
            Long num = 0l;
            if (list != null && list.size() > 0) {
                for (Map<String, Object> entryMap : list) {
                    num = (Long) entryMap.get("count");
                    totalNum = totalNum + num;
                }
                Long value = totalNum;
                for (Map<String, Object> entryMap2 : list) {
                    value = value - (Long) entryMap2.get("count");
                    entryMap2.put("value", value);
                    if (totalNum.equals((Long) entryMap2.get("count"))) {
                        entryMap2.replace("value", 0l);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
        return ResultUtil.returnSuccess("成功", list);
    }

    /**
     * 主体责任排行榜
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> findRanks(Map map) {
        Map<String, Object> maps = new HashMap<>();
        try {

            String partyId = map.get("partyId").toString();
            // 通过id判断是市级单位还是局已经分局
            String limit = decideInfo(partyId);
            if("0".equals(limit)){
                // 0、1代表的是市、局
                // 查询维保单位
                String param = "parent_id";
                Map<String,Object> paramap = new HashMap<>();
                paramap.put("partyId",partyId);
                paramap.put("type",1);
                paramap.put("param",param);

                List<Map<String, Object>> mainRank = indexEchartsMapp.maintenanceRanksCity(paramap);

                // 查询使用单位
                paramap.put("type",2);
                List<Map<String, Object>> userRank = indexEchartsMapp.maintenanceRanksCity(paramap);

                //查询维保单位用户
                paramap.put("type",1);
                List<Map<String, Object>> mainUserRank = indexEchartsMapp.maintenanceUserRanksCity(paramap);

                //查询使用单位用户
                paramap.put("type",2);
                List<Map<String, Object>> userUserRank = indexEchartsMapp.maintenanceUserRanksCity(paramap);

                maps.put("mainRank", mainRank);
                maps.put("userRank", userRank);
                maps.put("mainUserRank", mainUserRank);
                maps.put("userUserRank", userUserRank);

            }
            // 后面还不知有什么权限等级,放弃else
            if("1".equals(limit) || "2".equals(limit)
            ){
                String param = "id";

                Map<String,Object> paramap = new HashMap<>();
                paramap.put("partyId",partyId);
                paramap.put("type",1);
                paramap.put("param",param);
                //分局查询上级局的ID
                if("2".equals(limit)){
                    partyId = partyMapp.infoById(partyId).getParentId();
                }
                // 查询维保单位
                List<Map<String, Object>> mainRank = indexEchartsMapp.maintenanceRanksCity(paramap);

                // 查询使用单位
                paramap.put("type",2);
                List<Map<String, Object>> userRank = indexEchartsMapp.maintenanceRanksCity(paramap);

                //查询维保单位用户
                paramap.put("type",1);
                List<Map<String, Object>> mainUserRank = indexEchartsMapp.maintenanceUserRanksCity(paramap);

                //查询使用单位用户
                paramap.put("type",2);
                List<Map<String, Object>> userUserRank = indexEchartsMapp.maintenanceUserRanksCity(paramap);

                maps.put("mainRank", mainRank);
                maps.put("userRank", userRank);
                maps.put("mainUserRank", mainUserRank);
                maps.put("userUserRank", userUserRank);


            }
        } catch (Exception e) {
            logger.error("排行榜查询：" + e.getMessage());
            return ResultUtil.returnError(e.getMessage(), null);
        }
        return ResultUtil.returnSuccess("成功", maps);
    }

    /**
     * 获取是否是分局信息
     * @param id
     * @return
     * @throws Exception
     */
    private String decideInfo(String id) {
        String limit = "0";
        // 判断市或者局  通过传入Id判断  0是市级 1是局级 2 是分局
        List<Party> partys = partyMapp.decideBranchInfo(id);
        if (partys != null && partys.size() > 0) {
            List<Party> partyList = partyMapp.decideForEach(id);
            if (partyList != null && partyList.size() > 0) {
                limit = "0";
            } else {
                // 此时为局
                limit = "1";
            }
        } else {
            limit = "2";
        }
        return limit;
    }
}
